package zero;

import java.io.DataInputStream;
import java.io.FileOutputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * 基于IO的数据传输 零拷贝数据传输
 * @author yunlongn
 */
public class OldServer {
    public static void main(String[] args) throws Exception {
        ServerSocket serverSocket = new ServerSocket(8899);


        while (true){
            Socket socket = serverSocket.accept();

            String outPath = "D:\\out";
            FileOutputStream fileOutputStream = new FileOutputStream(outPath);
            DataInputStream dataInputStream = new DataInputStream(socket.getInputStream());

            try {
                byte[] bytes = new byte[4096];
                while (true) {
                    int read = dataInputStream.read(bytes, 0, bytes.length);
                    if (-1 == read) {
                        fileOutputStream.flush();
                        fileOutputStream.close();
                        break;
                    }
                    fileOutputStream.write(bytes);
                }
            }
            catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
